package com.frank.demo.controller;/*
 *@author:xec
 *@date:2022/2/28  8:58
 */

import com.alibaba.fastjson.JSONObject;
import com.frank.demo.domain.User;
import com.frank.demo.req.UserLoginReq;
import com.frank.demo.req.UserQueryReq;
import com.frank.demo.req.UserResetPasswordReq;
import com.frank.demo.req.UserSaveReq;
import com.frank.demo.resp.CommonResp;
import com.frank.demo.resp.UserLoginResq;
import com.frank.demo.resp.UserQueryResp;
import com.frank.demo.resp.PageResp;
import com.frank.demo.service.UserService;
import com.frank.demo.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *  对user表的操作都在此接口下执行
 */

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 查询所有电子书的方法
     * @return
     */
    @GetMapping("/getAllUser")
    public CommonResp<List<User>> selectAllUser(){
        List<User> list = userService.getAll();
        CommonResp<List<User>> resp = new CommonResp<List<User>>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return resp;
    }

    /**
     * 带条件查询电子书
     * @param userQueryReq
     * @return
     * 注解 @Valid 开启规则检验
     */
    @GetMapping("/getUser")
    public CommonResp<PageResp<UserQueryResp>> selectEBook(@Valid UserQueryReq userQueryReq){
        PageResp<UserQueryResp> list = userService.getUser(userQueryReq);
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return resp;
    }

    /**
     * 保存方法
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@RequestBody UserSaveReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));// 密码不明文存储，使用md5加密
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        userService.delete(id);
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@RequestBody UserLoginReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        UserLoginResq userLoginResq = userService.login(req);
        Long token = snowFlake.nextId();
        userLoginResq.setToken(token.toString());
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResq),3600*24, TimeUnit.SECONDS);
        resp.setContent(userLoginResq);
        return resp;
    }

    /**
     * 重置密码
     * @param req
     * @return
     */
    @PostMapping("/reset-password")
    public CommonResp resetPassword(@RequestBody UserResetPasswordReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));// 密码不明文存储，使用md5加密
        CommonResp resp = new CommonResp<>();
        userService.resetPassword(req);
        return resp;
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token) {
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);//把存在redis里面的token移除即可
        LOG.info("从redis中移除token：{}",token);
        return resp;
    }
}
